const { app,BrowserWindow,ipcMain  } = require('electron')
const path = require('node:path')

function createWindows(){
    //当app准备好后，执行createWindow创建窗口

    const win = new BrowserWindow({
        width: 800,//窗口宽度
        height: 600,//窗口高度
        autoHideMenuBar: true,//自动隐藏菜单档
        alwaysOnTop: true,//置顶
        x: 0,//窗口位置x坐标
        y: 0,//窗口位置y坐标
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            devTools:true,//调试模式
            nodeIntegration: true,
        }

    })
    // 给按钮添加事件监听
    win.webContents.on('did-frame-finish-load', () => {
        // 当页面加载完成后打开开发者工具
        win.webContents.openDevTools();
    });

    ipcMain.on('set-title', (event, title) => {
        const webContents = event.sender
        const win = BrowserWindow.fromWebContents(webContents)
        win.setTitle(title)
        console.log(title)
    })

    //加载一个远程页面
    win.loadFile('./pages/index/index.html')
}

app.on('ready', () => {
    createWindows()
})

app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindows()
    }
})
app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})